package gw.com.android.presenter;

import android.app.Activity;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gwtsz.android.rxbus.RxBus;
import com.gwtsz.gts2.hx.R;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.GTConfig;
import gw.com.android.model.ConfigType;
import gw.com.android.ui.dialog.AdsPopWindow;
import gw.com.android.utils.NetUtil;
import gw.com.jni.library.terminal.GTSConst;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import www.com.library.model.DataItemDetail;

/**
 * 首页fragment部分逻辑处理类
 * Created by reeta.zou on 2017/5/15.
 */
public class HomePresenter extends BasePresenter {

    public AdsPopWindow popWindow = null;
    private boolean upgradeAppeared = false;// 升级弹窗之后，再显示首页弹窗广告
    private CompositeDisposable compositeDisposable;

    public HomePresenter(Activity context) {
        this.mContext = context;
        Disposable subscribe = RxBus.getInstance().register(GTSConst.REPLY_POP_ADS_AFTER_UGPRADE, Integer.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(@NonNull Integer bundle) throws Exception {
                        if (compositeDisposable != null) {
                            compositeDisposable.clear();
                            compositeDisposable = null;
                        }
                        if (upgradeAppeared) {
                            return;
                        }
                        upgradeAppeared = true;
                        if (!GTConfig.instance().loadedAdsPop) {
                            dealPopAdsData();
                        }
                    }
                });
        compositeDisposable = new CompositeDisposable();
        compositeDisposable.add(subscribe);
    }

    /**
     * 底部开护栏广告
     */
    public void dealBottomAdsData(AdsData bottomAd, TextView textView) {
        if (bottomAd == null || bottomAd.adPics == null || bottomAd.adPics.isEmpty()) {
            return;
        }
        final AdsData.PicData picData = bottomAd.adPics.get(0);
        if (picData == null || TextUtils.isEmpty(picData.title)) {
            return;
        }
        textView.setText(picData.title);
    }

    /**
     * 处理图片数据
     */
    public void dealPopAdsData() {
        if (!upgradeAppeared) {
            // 升级弹窗之后，再显示首页弹窗广告
            return;
        }
        final String key = "pref_popads_" + GTConfig.instance().mCurName;
        if (GTConfig.instance().getAccountType() != GTConfig.ACC_TYPE_GUEST) {
            long longValue = GTConfig.instance().getLongValue(key, 0L);
            if (DateUtils.isToday(longValue)) {
                return;
            }
        }
        AdsData popAd = AdsConfigPresenter.getInstance().getAd(AdsConfigPresenter.TYPE_HOME_POP);
        if (popAd == null || popAd.adPics == null || popAd.adPics.isEmpty()) {
            return;
        }
        final AdsData.PicData picData = popAd.adPics.get(0);
        if (picData == null || TextUtils.isEmpty(picData.link) || TextUtils.isEmpty(picData.url)) {
            return;
        }
        GTConfig.instance().loadedAdsPop = true;
        if (null != mContext && !mContext.isFinishing()) {
            mContext.runOnUiThread(new Runnable() {
                @Override
                public void run() {
//                    Logger.e("弹层广告处理 ====  runOnUiThread");
                    if (null == popWindow) {
                        popWindow = new AdsPopWindow(mContext);
                        popWindow.setCancelable(false);
                    }
                    popWindow.setImageView(picData);
                    if (!popWindow.isShowing()) {
                        popWindow.show();
                        if (GTConfig.instance().getAccountType() != GTConfig.ACC_TYPE_GUEST) {
                            GTConfig.instance().setLongValue(key, System.currentTimeMillis());
                        }
                    }
                }
            });
        }
    }

    /**
     * 真实模拟的副标题文案- 两个一起配置
     */
    public void dealOpenAccountAd(AdsData ad, TextView tvDemo, TextView tvReal) {
        if (ad == null || tvReal == null || tvDemo == null) {
            return;
        }
        if (ad.adPics == null || ad.adPics.size() == 0) {
            return;
        }
        AdsData.PicData picData = ad.adPics.get(0);
        if (picData != null) {
            tvDemo.setText(picData.title);
        }
        if (ad.adPics.size() > 1) {
            picData = ad.adPics.get(1);
            if (picData != null) {
                tvReal.setText(picData.title);
            }
        }
    }

    /**
     * 处理 新手指南-关于恒信 这里，只考虑两张图都配置了的情况，只要发现数据为空，或者组件为空，就不显示整个父组件
     */
    public void dealNewbieAndAboutUs(final Activity activity, AdsData adsData, ImageView ivNewbie, ImageView ivAboutUs) {
        if (activity == null || ivNewbie == null || ivAboutUs == null) {
            return;
        }
        if (adsData == null || adsData.adPics == null || adsData.adPics.isEmpty()) {//没有缓存的情况下
            if (!NetUtil.isNetworkAvailable(activity)) {//如果网络不可用
                ivNewbie.setVisibility(View.VISIBLE);
                ivAboutUs.setVisibility(View.VISIBLE);//就显示占位图
            } else {//如果可用
                ivNewbie.setVisibility(View.GONE);//就让view消失
                ivAboutUs.setVisibility(View.GONE);
            }
        } else {
            ivNewbie.setVisibility(View.VISIBLE);
            ivAboutUs.setVisibility(View.VISIBLE);

            final AdsData.PicData newbieData = adsData.adPics.get(0);
            Glide.with(activity)
                    .load(newbieData.url)
                    .placeholder(R.mipmap.a_default_adslip)
                    .error(R.mipmap.a_default_adslip)
                    .dontAnimate()//防止设置placeholder导致第一次不显示网络图片,只显示默认图片的问题
                    .into(ivNewbie);
            ivNewbie.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DataItemDetail detail = new DataItemDetail();
                    detail.setStringValue(ConfigType.CONFIG_TYPE_TYPE_URL_TAG, newbieData.link);
                    detail.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, newbieData.title);
                    ActivityManager.showWebPageActivity(activity, detail, null);
                }
            });

            if (adsData.adPics.size() <= 1) {
                return;
            }
            final AdsData.PicData aboutAd = adsData.adPics.get(1);
            Glide.with(activity)
                    .load(aboutAd.url)
                    .placeholder(R.mipmap.a_default_adslip)
                    .error(R.mipmap.a_default_adslip)
                    .dontAnimate()//防止设置placeholder导致第一次不显示网络图片,只显示默认图片的问题
                    .into(ivAboutUs);
            ivAboutUs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DataItemDetail detail = new DataItemDetail();
                    detail.setStringValue(ConfigType.CONFIG_TYPE_TYPE_URL_TAG, aboutAd.link);
                    detail.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, aboutAd.title);
                    ActivityManager.showWebPageActivity(activity, detail, null);
                }
            });
        }
    }

}
